﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Data.SqlClient;
using Net7Music_Api.Model;

namespace Net7Music_Api.DAL
{
    public class UserServices
    {
        //登录
        public static User UserLogin(string name, string pwd)
        {
            string sql = $"select * from [User] where Account = '{name}' or Phone = '{name}' and Password = '{pwd}'";
            SqlDataReader dr = DBHelper.GetDataReader(sql);
            User user = null;
            if (dr.Read())
            {
                user = new User()
                {
                    ID = dr.GetInt32(0),
                    Account = dr.GetString(1),
                    Password = dr.GetString(2),
                    Phone = dr.GetString(3),
                    Email = dr.GetString(4),
                    Nickname = dr.GetString(5),
                    HeadAddress = dr.GetString(6),
                    Home = dr.GetString(7),
                    Age = dr.GetInt32(8),
                    Introduce = dr.GetString(9),
                    Sex = dr.GetString(10),
                    Birthday = dr.GetDateTime(11),
                    Useridentity = dr.GetInt32(12),
                };
            }
            dr.Close();
            return user;
        }

        //精确查询
        public static List<User> UserByID(int id)
        {
            string sql = $"select * from [User] u,Playlist p where u.id=p.UserID and u.ID={id}";
            SqlDataReader dr = DBHelper.GetDataReader(sql);
            List<User> users = new();
            while (dr.Read())
            {
                User user = new()
                {
                    ID = dr.GetInt32(0),
                    Account = dr.GetString(1),
                    Password = dr.GetString(2),
                    Phone = dr.GetString(3),
                    Email = dr.GetString(4),
                    Nickname = dr.GetString(5),
                    HeadAddress = dr.GetString(6),
                    Home = dr.GetString(7),
                    Age = dr.GetInt32(8),
                    Introduce = dr.GetString(9),
                    Sex = dr.GetString(10),
                    Birthday = dr.GetDateTime(11),
                    Useridentity = dr.GetInt32(12),
                    PID = dr.GetInt32(13),
                    PlaylistName = dr.GetString(14),
                    PlaylistImg = dr.GetString(15),
                    UserID = dr.GetInt32(16),
                    Hits = dr.GetInt32(17),
                };
                users.Add(user);
            }
            return users;
        }
    }
}
